﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Collections;

public partial class forms_audiorecordings_audiorecordings : reportPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.initialize();
        base.ensureUserIsLoggedIn();

        Tree _Root = Tree.Instance;

        int nodeGuid = 0;
        int.TryParse(Request.QueryString["t"], out nodeGuid);
        Tree selectedTreeItem = _Root.All[nodeGuid] as Tree;

        if (selectedTreeItem == null || selectedTreeItem == _Root)
        {
            selectedTreeItem = _Root.FindChildByName(base.userProjectName);
        }

        StringBuilder tempBuilder = new StringBuilder();

        Tree parentItem = selectedTreeItem.Parent;
        while (parentItem != null && parentItem != _Root)
        {
            tempBuilder.Insert(0, string.Format("<a href=\"?t={1}\">{0}</a>&nbsp;/&nbsp;", ToTurkish(parentItem.Name), parentItem.UniqueId));
            parentItem = parentItem.Parent;
        }
        tempBuilder.Insert(0, "<tr><td colspan='2'><p>");
        tempBuilder.Append(string.Format("<span style=\"color:gray;\">{0}&nbsp;/&nbsp;</span>", ToTurkish(selectedTreeItem.Name)));
        tempBuilder.Append("</p></td></tr>");

        foreach (Tree treeItem in selectedTreeItem.Children)
        {
            if (treeItem.IsFile)
            {
                RenderFile(treeItem, tempBuilder);
            }
            else
            {
                if (treeItem.Children.Count > 0)
                {
                    tempBuilder.Append("<tr><td colspan='2'>");
                    if (treeItem.FileCount > 0)
                    {
                        tempBuilder.AppendFormat("<p style=\"margin-left:{0}0px;\"><a href=\"?t={3}\">{1} ({2} Kayıt)&nbsp;&nbsp;&nbsp;&nbsp;</a></p>", 1, ToTurkish(treeItem.Name), treeItem.Children.Count, treeItem.UniqueId);
                    }
                    else
                    {
                        tempBuilder.AppendFormat("<p style=\"margin-left:{0}0px;\"><a href=\"?t={3}\">{1}&nbsp;&nbsp;&nbsp;&nbsp;</a></p>", 1, ToTurkish(treeItem.Name), treeItem.Children.Count, treeItem.UniqueId);
                    }
                    tempBuilder.Append("</td></tr>");
                }
            }
        }
        audioFolderListTable.Text = tempBuilder.ToString();
    }


    static string ToTurkish(string folderName)
    {
        return folderName
             .Replace("Ý", "İ")
             .Replace("Ð", "Ğ")
             .Replace("Þ", "Ş");
    }

    private void RenderFile(Tree fileItem, StringBuilder htmlContents)
    {
        string fileNameOnly = Path.GetFileNameWithoutExtension(fileItem.Value);
        string[] fileNameOnlyArray = fileNameOnly.Split('-');
        int dealerCodeIndex = 0;
        string dealerName = fileNameOnlyArray[dealerCodeIndex].PadLeft(5, '0');
        try
        {
            dealerName = "(" + dealerName + ") " + base.getDataCache().getDealer(fileNameOnlyArray[dealerCodeIndex].PadLeft(5, '0')).getName();
        }
        catch { }

        htmlContents.AppendFormat
        (
            "<tr><td>{0}{1}, Görüşme ID: {2}</td><td>{3} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"{4}\")'><img src='/i/play.gif' border='0' /></a></td></tr>",
            "",
            dealerName,
            fileNameOnlyArray.Length < 2 ? "N/A" + fileNameOnlyArray[dealerCodeIndex] : fileNameOnlyArray[dealerCodeIndex + 1],
            fileNameOnly + ".wav",
            "/" + fileItem.Value.Replace(Server.MapPath("/"), "").ToLowerInvariant().Replace("/forms/audiorecordings", "").Replace("\\", "/")
        );
    }

















    //static StringBuilder _FoldersHtml;
    //static object _FoldersHtmlLock = new object();



    //StringBuilder FoldersHtml
    //{
    //    get
    //    {
    //        StringBuilder tempBuilder = new StringBuilder();
    //        int indent = 0;
    //        foreach (string subFolder in Directory.GetDirectories(HttpContext.Current.Server.MapPath("/m/" + base.userProjectName)))
    //        {
    //            Recurse(subFolder, tempBuilder, ref indent);
    //        }
    //        return tempBuilder;
    //    }
    //}



    //static void Recurse(string folder, StringBuilder htmlBuilder, ref int indent)
    //{
    //    int fileCount = Directory.GetFiles(folder, "*.wav").Length;
    //    htmlBuilder.Append("<tr><td colspan='2'>");
    //    if (fileCount > 0)
    //    {
    //        htmlBuilder.AppendFormat("<p style=\"margin-left:{0}0px;\">{1} ({2} Kayıt)&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"?f={3}\">Listele</a></p>", indent++, ToTurkish(Path.GetFileName(folder)), fileCount, Convert.ToBase64String(Encoding.UTF8.GetBytes(folder)));
    //    }
    //    else
    //    {
    //        htmlBuilder.AppendFormat("<p style=\"margin-left:{0}0px;\">{1}</p>", indent++, ToTurkish(Path.GetFileName(folder)));
    //    }
    //    htmlBuilder.Append("</td></tr>");
    //    foreach (string subFolder in Directory.GetDirectories(folder))
    //    {
    //        Recurse(subFolder, htmlBuilder, ref indent);
    //    }
    //    indent--;
    //}

    //protected void Page_Load(object sender, EventArgs e)
    //{
    //    base.initialize();
    //    base.ensureUserIsLoggedIn();

    //    if (Request.QueryString["f"] != null)
    //    {
    //        string folder = Encoding.ASCII.GetString(Convert.FromBase64String(Request.QueryString["f"]));
    //        RenderFileView(folder);
    //    }
    //    else
    //    {
    //        RenderFolderView();
    //    }

    //    base.RenderComplete();
    //    renderTime.Text = base.getElapsedRenderTime().ToString();
    //}

    //private void RenderFolderView()
    //{
    //    audioFolderListTable.Text = FoldersHtml.ToString();
    //}

}
